package com.example.demo.app;

import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.example.demo.app.exception.ServiceException;
import com.google.common.collect.Maps;

@ControllerAdvice
public class BookControllerAdvice {
	
	
	@ExceptionHandler(ServiceException.class)
    @ResponseBody
	public Map<String,String> handlerException(Exception ex){
		Map<String,String> message = Maps.newHashMap();
		message.put("error", "出现异常信息");
		message.put("ex", ex.getMessage());
		return message;
	}
	
    
	@ExceptionHandler(ArithmeticException.class)
	@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)//自定义返回code
	@ResponseBody
	public Map<String,String> handlerException2(Exception ex){
		Map<String,String> message = Maps.newHashMap();
		message.put("error2", "出现异常信息");
		message.put("ex2", ex.getMessage());
		return message;
	}
	

	@InitBinder
	private void initBinder(WebDataBinder binder){
		
	}
	
}
